package com.hopu.v1.controller.admin.system;


import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.common.Save;
import com.hopu.v1.pojo.common.Update;
import com.hopu.v1.pojo.system.Menu;
import com.hopu.v1.service.admin.system.MenuService;
import com.hopu.v1.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YwaiX
 * @since 2024-09-12
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping
    public List<Menu> getMenuByPidAndStatus(Integer pid, Integer status) {
        return menuService.selectByPidAndStatus(pid, status);
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @PostMapping("/update")
    public R updateById(@Validated(Update.class) @RequestBody Menu menu){

        return menuService.updateMenu(menu);
    }

    /**
     * 删除菜单
     * @param menu
     * @return
     */
    @PutMapping
    public R update(@RequestBody Menu menu) {
        return R.ok(this.menuService.removeById(menu.getId()));
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("/insert")
    public R insert(@Validated(Save.class)Menu menu , @RequestHeader String token) {
        menu.setCreateBy(Integer.valueOf(JwtUtils.getUserId(token)));
        menu.setCreateTime(LocalDateTime.now());
        this.menuService.insertMenu(menu);
        return R.ok("添加成功");
    }
}
